package controllers

import (
	"encoding/json"
	"strconv"
	"supershare/backend/middleware"
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// VenuePackageController 场地预约套餐管理控制器
type VenuePackageController struct {
	web.Controller
}

// VenuePackageListRequest 场地预约套餐列表请求参数
type VenuePackageListRequest struct {
	Page     int    `json:"page" form:"page"`
	PageSize int    `json:"pageSize" form:"pageSize"`
	Name     string `json:"name" form:"name"`
	Status   int8   `json:"status" form:"status"`
}

// VenuePackageCreateRequest 创建场地预约套餐请求参数
type VenuePackageCreateRequest struct {
	Name              string  `json:"name"`
	Description       string  `json:"description"`
	Price             float64 `json:"price"`
	Duration          int     `json:"duration"`
	Stores            string  `json:"stores"`
	Venues            string  `json:"venues"`
	Status            int8    `json:"status"`
	SortOrder         int     `json:"sort_order"`
	AvailableTimes    string  `json:"available_times"`
	AvailableWeekdays string  `json:"available_weekdays"`
}

// VenuePackageUpdateRequest 更新场地预约套餐请求参数
type VenuePackageUpdateRequest struct {
	Name              string  `json:"name"`
	Description       string  `json:"description"`
	Price             float64 `json:"price"`
	Duration          int     `json:"duration"`
	Stores            string  `json:"stores"`
	Venues            string  `json:"venues"`
	Status            int8    `json:"status"`
	SortOrder         int     `json:"sort_order"`
	AvailableTimes    string  `json:"available_times"`
	AvailableWeekdays string  `json:"available_weekdays"`
}

// GetOne 获取场地预约套餐详情
func (c *VenuePackageController) GetOne() {
	// 获取场地预约套餐ID
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	if id <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的场地预约套餐ID",
		}
		c.ServeJSON()
		return
	}
	pack, err := models.GetVenuePackageById(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "场地预约套餐不存在",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "获取场地预约套餐详情成功",
		"data":    pack,
	}
	c.ServeJSON()
}

// List 获取场地预约套餐列表
func (c *VenuePackageController) List() {
	// 获取请求参数
	var req VenuePackageListRequest
	if err := c.ParseForm(&req); err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "参数解析失败",
		}
		c.ServeJSON()
		return
	}

	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}
	// 设置默认值
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.PageSize <= 0 {
		req.PageSize = 10
	}

	// 构建过滤条件
	filters := make(map[string]interface{})
	if req.Name != "" {
		filters["name"] = req.Name
	}
	if req.Status > 0 {
		filters["status"] = req.Status
	}

	// 获取当前用户所属加盟商ID
	franchiseId := admin.FranchiseId
	if franchiseId > 0 {
		filters["franchise_id"] = franchiseId
	}

	// 获取场地预约套餐列表
	packs, total, err := models.GetVenuePackageList(req.Page, req.PageSize, filters)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取场地预约套餐列表失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "获取场地预约套餐列表成功",
		"data": map[string]interface{}{
			"list":     packs,
			"total":    total,
			"page":     req.Page,
			"pageSize": req.PageSize,
		},
	}
	c.ServeJSON()
}

// Create 创建场地预约套餐
func (c *VenuePackageController) Create() {
	// 解析请求参数
	var req VenuePackageCreateRequest
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "参数解析失败",
		}
		c.ServeJSON()
		return
	}

	// 参数验证
	if req.Name == "" {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入套餐名称",
		}
		c.ServeJSON()
		return
	}

	if req.Price <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入有效的套餐价格",
		}
		c.ServeJSON()
		return
	}

	if req.Duration <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入有效的使用时长",
		}
		c.ServeJSON()
		return
	}

	// 获取当前用户所属加盟商ID
	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}
	// 创建场地预约套餐
	pack := &models.VenuePackage{
		Name:              req.Name,
		Description:       req.Description,
		Price:             req.Price,
		Duration:          req.Duration,
		Stores:            req.Stores,
		Venues:            req.Venues,
		Status:            req.Status,
		SortOrder:         req.SortOrder,
		AvailableTimes:    req.AvailableTimes,
		AvailableWeekdays: req.AvailableWeekdays,
	}
	pack.FranchiseId = admin.FranchiseId
	pack.CreatedBy = admin.Username
	pack.UpdatedBy = admin.Username
	pack.BeforeInsert()
	id, err := models.CreateVenuePackage(pack)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "创建场地预约套餐失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "创建场地预约套餐成功",
		"data": map[string]interface{}{
			"id": id,
		},
	}
	c.ServeJSON()
}

// Update 更新场地预约套餐
func (c *VenuePackageController) Update() {
	// 获取场地预约套餐ID
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	if id <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的场地预约套餐ID",
		}
		c.ServeJSON()
		return
	}

	// 解析请求参数
	var req VenuePackageUpdateRequest
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "参数解析失败",
		}
		c.ServeJSON()
		return
	}

	// 参数验证
	if req.Name == "" {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入套餐名称",
		}
		c.ServeJSON()
		return
	}

	if req.Price <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入有效的套餐价格",
		}
		c.ServeJSON()
		return
	}

	if req.Duration <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "请输入有效的使用时长",
		}
		c.ServeJSON()
		return
	}
	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}
	franchiseId := admin.FranchiseId
	// 获取场地预约套餐信息
	pack, err := models.GetVenuePackageById(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "场地预约套餐不存在",
		}
		c.ServeJSON()
		return
	}

	// 更新场地预约套餐信息
	pack.Name = req.Name
	pack.Description = req.Description
	pack.Price = req.Price
	pack.Duration = req.Duration
	pack.Stores = req.Stores
	pack.Venues = req.Venues
	pack.Status = req.Status
	pack.SortOrder = req.SortOrder
	pack.AvailableTimes = req.AvailableTimes
	pack.AvailableWeekdays = req.AvailableWeekdays
	pack.FranchiseId = franchiseId
	pack.UpdatedBy = admin.Username
	pack.BeforeUpdate()
	pack.FranchiseId = franchiseId
	err = models.UpdateVenuePackage(pack, "Name", "Description", "Price", "Duration", "FranchiseId", "Stores", "Venues", "Status", "SortOrder", "AvailableTimes", "AvailableWeekdays", "UpdatedBy", "UpdatedAt")
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "更新场地预约套餐失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "更新场地预约套餐成功",
	}
	c.ServeJSON()
}

// Delete 删除场地预约套餐
func (c *VenuePackageController) Delete() {
	// 获取场地预约套餐ID
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	if id <= 0 {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的场地预约套餐ID",
		}
		c.ServeJSON()
		return
	}
	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}
	franchiseId := admin.FranchiseId
	// 获取场地预约套餐信息
	pack, err := models.GetVenuePackageById(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "场地预约套餐不存在",
		}
		c.ServeJSON()
		return
	}
	if pack.FranchiseId != franchiseId {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "您没有权限删除该场地预约套餐",
		}
		c.ServeJSON()
		return
	}
	// 删除场地预约套餐
	err = models.DeleteVenuePackage(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "删除场地预约套餐失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "删除场地预约套餐成功",
	}
	c.ServeJSON()
}
